package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.spzx.common.core.web.domain.AjaxResult.success;

/**
 * @author LOGIN
 * @title CartController
 * @create 2025/7/25 22:39
 * @description
 */
@Tag(name="购物车接口")
@RestController
@RequestMapping
public class CartController {

    @Autowired
    private CartService cartService;

    @Operation(summary = "添加购物车列表")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    @RequiresLogin
    public AjaxResult addToCart(
            @Parameter(description = "商品的skuId")
            @PathVariable("skuId") Long skuId,
            @Parameter(description = "商品的数量")
            @PathVariable("skuNum") Integer skuNum
    ){
        cartService.addToCart(skuId,skuNum) ;
        return success() ;
    }
    @Operation(summary = "查询购物车列表")
    @GetMapping("/cartList")
    @RequiresLogin
    public AjaxResult cartList(){
        return success(cartService.cartList()) ;
    }
    @Operation(summary = "删除购物车列表")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(
            @Parameter(description = "商品的skuId")
            @PathVariable("skuId") Long skuId
    ){
        cartService.deleteCart(skuId);
        return success();
    }
    @Operation(summary = "更新选中状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(
            @Parameter(description = "商品skuId")
            @PathVariable("skuId") Long skuId,
            @Parameter(description = "是否选中 1:选中 0:取消选中")
            @PathVariable("isChecked") Integer isChecked
    ){
        cartService.checkCart(skuId,isChecked);
        return success();
    }
    @Operation(summary = "全选")
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(
            @Parameter(description = "是否选中 1:选中 0:取消选中")
            @PathVariable("isChecked") Integer isChecked
    ){
        cartService.allCheckCart(isChecked);
        return success();
    }
    @Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }

    @Operation(summary = "查询用户购物车列表中选中商品列表")
    @GetMapping("/getCartCheckedList")
    public R<List<CartInfo>> getCartCheckedList(){
               return R.ok(cartService.getCartCheckedList());
    }

    @Operation(summary = "删除用户购物车列表中选中商品列表")
    @InnerAuth(isUser = true)
    @GetMapping("/deleteCartCheckedList")
    public R<Boolean> deleteCartCheckedList(){
        return R.ok(cartService.deleteCartCheckedList());
    }

    @Operation(summary = "更新用户购物车列表价格")
    @InnerAuth(isUser = true)
    @GetMapping("/updateCartPrice")
    public R<Boolean> updateCartPrice(){
        return R.ok(cartService.updateCartPrice());
    }

}
